home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / muds / pennmush.000 / pennmush-1.50-p8-linux.tar / pennmush / wild.c < prev    next >
C/C++ Source or Header  |  1993-01-12  |  4KB  |  155 lines

  1. /* wild.c */
  2.  
  3.  
  4. /* wild card routine(s) created by Lawrence Foard */
  5. #include <stdio.h>
  6. #include <ctype.h>
  7. #include "config.h"
  8. #include "interface.h"
  9. #include "globals.h"
  10.  
  11. char *wptr[10];
  12. int wlen[10];
  13. static char wbuff[BUFFER_LEN];
  14.  
  15. static char *lwptr[10];
  16. static int lwlen[10];
  17. static char lwbuff[BUFFER_LEN];
  18.  
  19. int wild(s, d, p, os)
  20.     char *s;
  21.     char *d;
  22.     int p;            /* what argument are we on now? */
  23.     int os;            /* true if just came from wild card state */
  24. {
  25.   switch (*s) {
  26.     case '?':            /* match any character in d, note end of
  27.                  * string is considered a match */
  28.       /* if just in nonwildcard state record location of change */
  29.       if (!os && (p < 10))
  30.     wptr[p] = d;
  31.       return (wild(s + 1, (*d) ? d + 1 : d, p, 1));
  32.     case '*':            /* match a range of characters */
  33.       if (!os && (p < 10)) {
  34.     wptr[p] = d;
  35.       }
  36.       return (wild(s + 1, d, p, 1) || ((*d) ? wild(s, d + 1, p, 1) : 0));
  37.     default:
  38.       if (os && (p < 10)) {
  39.     wlen[p] = d - wptr[p];
  40.     p++;
  41.       }
  42.       return ((DOWNCASE(*s) != DOWNCASE(*d)) ? 0 :
  43.           ((*s) ? wild(s + 1, d + 1, p, 0) : 1));
  44.   }
  45. }
  46.  
  47. int wild_match(s, d)
  48.     char *s;
  49.     char *d;
  50. {
  51.   /* s is a wildcard pattern. Return 1 if d matches it. */
  52.  
  53.   int a;
  54.   for (a = 0; a < 10; a++)
  55.     wptr[a] = NULL;
  56.  
  57.   switch (*s) {
  58.     case '>':
  59.       s++;
  60.       /* if both first letters are #'s then numeric compare */
  61.       if ((isascii(s[0]) && isdigit(s[0])) || (*s == '-'))
  62.     return (atoi(s) < atoi(d));
  63.       else
  64.     return (strcmp(s, d) < 0);
  65.     case '<':
  66.       s++;
  67.       if ((isascii(s[0]) && isdigit(s[0])) || (*s == '-'))
  68.     return (atoi(s) > atoi(d));
  69.       else
  70.     return (strcmp(s, d) > 0);
  71.     default:
  72.       if (wild(s, d, 0, 0)) {
  73.     int b;
  74.     char *e, *f = wbuff;
  75.     for (a = 0; a < 10; a++)
  76.       if ((e = wptr[a]) != NULL) {
  77.         wptr[a] = f;
  78.         for (b = wlen[a]; b--; *f++ = *e++) ;
  79.         *f++ = 0;
  80.       }
  81.     return (1);
  82.       } else
  83.     return (0);
  84.   }
  85. }
  86.  
  87. int local_wild(s, d, p, os)
  88.     char *s;
  89.     char *d;
  90.     int p;            /* what argument are we on now? */
  91.     int os;            /* true if just came from wild card state */
  92. {
  93.   switch (*s) {
  94.     case '?':            /* match any character in d, note end of
  95.                  * string is considered a match */
  96.       /* if just in nonwildcard state record location of change */
  97.       if (!os && (p < 10))
  98.     lwptr[p] = d;
  99.       return (local_wild(s + 1, (*d) ? d + 1 : d, p, 1));
  100.     case '*':            /* match a range of characters */
  101.       if (!os && (p < 10)) {
  102.     lwptr[p] = d;
  103.       }
  104.       return (local_wild(s + 1, d, p, 1) || 
  105.           ((*d) ? local_wild(s, d + 1, p, 1) : 0));
  106.     default:
  107.       if (os && (p < 10)) {
  108.     lwlen[p] = d - lwptr[p];
  109.     p++;
  110.       }
  111.       return ((DOWNCASE(*s) != DOWNCASE(*d)) ? 0 :
  112.           ((*s) ? local_wild(s + 1, d + 1, p, 0) : 1));
  113.   }
  114. }
  115.  
  116. int local_wild_match(s, d)
  117.     char *s;
  118.     char *d;
  119. {
  120.   /* s is a wildcard pattern. Return 1 if d matches it. */
  121.  
  122.   int a;
  123.   for (a = 0; a < 10; a++)
  124.     lwptr[a] = NULL;
  125.  
  126.   switch (*s) {
  127.     case '>':
  128.       s++;
  129.       /* if both first letters are #'s then numeric compare */
  130.       if ((isascii(s[0]) && isdigit(s[0])) || (*s == '-'))
  131.     return (atoi(s) < atoi(d));
  132.       else
  133.     return (strcmp(s, d) < 0);
  134.     case '<':
  135.       s++;
  136.       if ((isascii(s[0]) && isdigit(s[0])) || (*s == '-'))
  137.     return (atoi(s) > atoi(d));
  138.       else
  139.     return (strcmp(s, d) > 0);
  140.     default:
  141.       if (local_wild(s, d, 0, 0)) {
  142.     int b;
  143.     char *e, *f = lwbuff;
  144.     for (a = 0; a < 10; a++)
  145.       if ((e = lwptr[a]) != NULL) {
  146.         lwptr[a] = f;
  147.         for (b = lwlen[a]; b--; *f++ = *e++) ;
  148.         *f++ = 0;
  149.       }
  150.     return (1);
  151.       } else
  152.     return (0);
  153.   }
  154. }
  155.